package tests.example;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author: SunAo
 * @create: 2020-08-21 18:42
 */
public class Random2 {

    Map<String,Integer> map = new HashMap<>();

    {
        map.put("1",20);
        map.put("2",30);
    }


    Random random = new Random();
    public String cal1(){
        int count = 0 ;
        for (String key : map.keySet()) {
            count+=map.get(key);
        }
        int r = random.nextInt(count+1);
        System.out.println(r);
        for (String k : map.keySet()) {
            if(r < map.get(k)){
                return k;
            }
        }
        return null;
    }
    @Test
    public void cal1Test(){
        int s1 = 0 , s2 = 0 ;
        for (int i = 0; i < 10000; i++) {
            if(cal1().equals("1")){
                s1++;
            }else{
                s2++;
            }
        }
        System.out.println("s1:"+s1+",s2:"+s2);
    }

}
